import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author YuanYuan
 * @Date 2022/11/26
 * @Time 10:21
 * +--------------+-------------+------+-----+---------+----------------+
 * | Field        | Type        | Null | Key | Default | Extra          |
 * +--------------+-------------+------+-----+---------+----------------+
 * | userId       | int(11)     | NO   | PRI | NULL    | auto_increment |
 * | userName     | varchar(50) | YES  | UNI | NULL    |                |
 * | userPassword | varchar(50) | YES  |     | NULL    |                |
 * +--------------+-------------+------+-----+---------+----------------+
 */

public class UserDAO {
    //插入数据
    public static void insert(User user) throws SQLException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "insert into user values(null,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getUserName());
            preparedStatement.setString(2,user.getUserPassword());
            int ret = preparedStatement.executeUpdate();
            if(0 != ret) {
                System.out.println("插入成功!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection, preparedStatement, null);
        }
    }
    //根据用户名查询---登录
    public static User selectByName(String username) throws SQLException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from user where UserName = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            resultSet = preparedStatement.executeQuery();
            if(resultSet.next()) {
                user = new User();
                user.setUserId(resultSet.getByte("userId"));
                user.setUserName(resultSet.getString("userName"));
                user.setUserPassword(resultSet.getString("userPassword"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection, preparedStatement, resultSet);
        }
        return user;
    }

    //根据用户ID查询
    public static User selectById(int userid) throws SQLException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from user where userId = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, userid);
            resultSet = preparedStatement.executeQuery();
            if(resultSet.next()) {
                user = new User();
                user.setUserId(resultSet.getByte("userId"));
                user.setUserName(resultSet.getString("userName"));
                user.setUserPassword(resultSet.getString("userPassword"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection, preparedStatement, resultSet);
        }
        return user;
    }

}
